package com.cocotea.valid;

import com.cocotea.valid.constant.MessageConstant;
import com.cocotea.valid.handler.ValidHandler;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class ValidMain {
    public static String valid(Class<?>[] parameterTypes, Object[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, ClassNotFoundException, InstantiationException {
        // controller方法的参数
        for (Class<?> cl : parameterTypes) {
            Field[] declaredFields = cl.getDeclaredFields();
            for (Field field : declaredFields) {
                if (field.getDeclaredAnnotations().length == 0) continue;
                String dispatch = ValidHandler.dispatch(field, args[0], cl);
                if (!dispatch.equals(MessageConstant.SUCCESS)) {
                    return dispatch;
                }
            }
        }
        return MessageConstant.SUCCESS;
    }
}
